package com.wzp.puppet;

import android.graphics.Point;
import android.hardware.display.IDisplayManager;
import android.os.Build.VERSION;
import android.os.IBinder;
import android.view.DisplayInfo;
import android.view.IWindowManager;
import android.view.IWindowManager.Stub;

import java.lang.reflect.Method;

public class SurfaceControlVirtualDisplayFactory {
    private static final String LOGTAG = "SCVDF";

    public static Point getCurrentDisplaySize(boolean rotate) {
        try {
            Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
            Point displaySize = new Point();
            IWindowManager wm;
            int rotation;
            if (VERSION.SDK_INT >= 18) {
                wm = Stub.asInterface((IBinder) getServiceMethod.invoke(null, new Object[]{"window"}));
                wm.getInitialDisplaySize(0, displaySize);

            } else if (VERSION.SDK_INT == 17) {
                DisplayInfo di = IDisplayManager.Stub.asInterface((IBinder) getServiceMethod.invoke(null, new Object[]{"display"})).getDisplayInfo(0);
                displaySize.x = ((Integer) DisplayInfo.class.getDeclaredField("logicalWidth").get(di)).intValue();
                displaySize.y = ((Integer) DisplayInfo.class.getDeclaredField("logicalHeight").get(di)).intValue();
                rotation = ((Integer) DisplayInfo.class.getDeclaredField("rotation").get(di)).intValue();
            } else {
                wm = Stub.asInterface((IBinder) getServiceMethod.invoke(null, new Object[]{"window"}));
                wm.getRealDisplaySize(displaySize);

            }

            return displaySize;
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
}